<template>
	<neil-modal :show="isShow" @close="onClose" title="提示" confirm-text="关闭" :show-cancel="false">
		<view class="text-center">
			<text>请授权个人信息</text>
		</view>
		<view class="login">
			<button type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo">授权</button>
		</view>
	</neil-modal>
</template>

<script>
	import neilModal from '@/components/neil-modal.vue'
	export default {
		name: 'user-login',
		components: {
			neilModal
		},
		props: {
			show: {
				type: Boolean,
				default: false
			}
		},
		data: function() {
			return {
				isShow: false
			}
		},
		created() {
			this.isShow = this.show
		},
		watch: {
			show(val) {
				this.isShow = val
			}

		},
		methods: {
			getuserinfo: async function(e) {
				if (e.detail.errMsg !== "getUserInfo:ok") return
				let userinfo = this.$store.state.userinfo
				let detail = e.detail.userInfo
				let user = { ...userinfo,
					...{
						nickName: detail.nickName,
						faceUrl: detail.avatarUrl,
						city: detail.city,
						country: detail.country,
						gender: detail.gender,
						language: detail.language,
						province: detail.province
					}
				}
				var {
					data
				} = await this.$http.post('/wxuser/grant', user)
				this.$store.commit('setUserInfo', data.data)
				this.$store.commit('setIsLogin', true)
				uni.showToast({
					title: '登录成功'
				})

				this.onClose();
			},
			onClose: function() {
				this.isShow = false
				this.$emit('close')
			}
		}
	}
</script>

<style>
	.text-center {
		padding: 20upx;
		text-align: center;
	}

	.login {
		padding: 20upx 50upx;
	}
</style>
